<?php
namespace app\controller;

use app\BaseController;
use app\model\Num;
use app\model\User;
use think\App;
use think\facade\Session;
use think\facade\View;

class Data extends BaseController
{

    private $uid = '';
    private $name = '';
    private $username = '';
    public function __construct(App $app)
    {
        parent::__construct($app);
        $uid = Session::get('uid');
        if(!$uid){
            return $this->error("请先登录","/index/index");
        }else{
            $this->uid = Session::get('uid');
            $this->name = Session::get('name');
            $this->username = Session::get('username');

        }

    }

    public function index()
    {
        if($this->uid != 1){
            $this->error('你不是管理员，无权查看！');
        }
        $num = Num::order('id desc')->find();
        View::assign('num',$num);
        if($num){
            $list = \app\model\Data::with('user')->where(['num'=>$num->id])->order('id desc')->select();
        }else{
            $list = [];
        }

        View::assign('list',$list);
        return View::fetch('index');
    }
    public function over()
    {
        $num = Num::order('id desc')->find();
        $num->enable = 1;
        $num->save();
        return redirect(url('/data/index'));
    }
    public function start()
    {
        $title['title'] = $this->request->post('title');

        $num = Num::create($title);
        return redirect(url('/data/index'));
    }

    public function create()
    {
        $num = Num::order('id desc')->find();
        View::assign('num',$num);
        if($num){
            $commit = \app\model\Data::where(['uid'=>$this->uid,'num'=>$num->id])->order('id desc')->find();
        }else{
            $commit = 0;
        }
        View::assign('commit',$commit);
        return View::fetch('create');
    }


    public function postdata(){
        $file = $this->request->file('image');
        if($file){
            $num = Num::order('id desc')->find($this->request->post('num'));
            if(!$num){
                $this->error("找不到投标");

            }
            $filename = $num->title."-".$this->name;
            $save = \think\facade\Filesystem::disk('public')->putFileAs("uploads",$file,$filename.".".$file->extension());
            if($save){
                // 成功上传后 获取上传信息
                // 输出 jpg
                $save = str_replace("\\","/",$save);
            }else{
                $this->error("图片未找到");
            }
            // 上传失败获取错误信息
            $data['uid'] = $this->uid;
            $data['num'] = $num->id;
            $data['title'] = $save;
            $data['createdAt'] = date('Y-m-d H:i:s');
            $data['createdAt'] = date('Y-m-d H:i:s');

            $res = \app\model\Data::create($data);
            if($res){
                $this->error("提交成功");
            }else{
                $this->error("提交失败");
            }
        }else{

            $this->error("图片未找到");
        }
    }
    public function editpass(){

        return View::fetch('editpass');
    }
    public function editpassword(){
        $opassword = $this->request->post('opassword');
        $password = $this->request->post('password');
        $password2 = $this->request->post('password2');
        $user = User::find($this->uid);
        if($user->password != userPass($opassword)){
            $this->error("原密码错误");
        }
        if($password != $password2){
            $this->error("两次输入密码不一致");
        }
        $user->password = userPass($password);
        $user->save();
        $this->success("密码修改成功",'/data/create');

    }
}
